package com.leetcode.p0;

import com.utils.DataUtils;

public class Test64 {

    public int minPathSum(int[][] grid) {
        help(grid, 0);
        return grid[grid.length - 1][grid[grid.length - 1].length - 1];
    }

    private void help(int[][] grid, int index) {
        if (grid.length == index) {
            return;
        }

        for (int i = 0; i < grid[index].length; i++) {
            if (index == 0) {
                if (i != 0) {
                    grid[index][i] = grid[index][i] + grid[index][i - 1];
                }
            } else {
                if (i == 0) {
                    grid[index][i] = grid[index][i] + grid[index - 1][i];
                } else {
                    grid[index][i] = grid[index][i] + Math.min(grid[index][i - 1], grid[index - 1][i]);
                }
            }
        }
        help(grid, index + 1);
    }

    public static void main(String[] args) {
        int[][] grid = DataUtils.string2DoubleArrayInteger("[[1,3,1],[1,5,1],[4,2,1]]");

        Test64 test = new Test64();
        int result = test.minPathSum(grid);
        System.out.println(result);
    }

}
